package com.job.task;

import com.job.dao.DataPipline;
import com.job.pojo.JobInfo;
import com.job.utils.MathSalary;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * @author Surpass
 * @Package com.job.task
 * @Description: ${todo}
 * @date 2020/4/30 16:36
 */
@Component
public class JobProcessor implements PageProcessor {

    @Autowired
    private DataPipline dataPipline;

    private String url = "https://search.51job.com/list/010000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";

    @Override
    public void process(Page page) {
        List<Selectable> list = page.getHtml().css("div#resultList div.el").nodes();
        if (list.size()==0){
            //详情页
            this.saveJobInfo(page);
        }else {
            //列表页
            for (Selectable selectable : list) {
                //获取url地址
                String jobInfoUrl = selectable.links().toString();
                //把url放入任务队列中
                page.addTargetRequest(jobInfoUrl);
            }
            //获取下一页url
            String s = page.getHtml().css("div.p_in li.bk").nodes().get(1).links().toString();
            //下一页放入任务队列
            page.addTargetRequest(s);
        }
    }

    //解析招聘详情信息
    public void saveJobInfo(Page page){
        Html html = page.getHtml();
        if (html==null){
            return;
        }

        JobInfo jobInfo = new JobInfo();

        String companyName = html.css("div.cn p.cname a", "text").toString();
        jobInfo.setCompanyName(companyName);
        /*String companyAddr = Jsoup.parse(html.css("div.bmsg p.fp").nodes().get(0).toString()).text();
        jobInfo.setCompanyAddr(companyAddr);*/
        String companyInfo = Jsoup.parse(html.css("div.tmsg").toString()).text();
        jobInfo.setCompanyInfo(companyInfo);
        String jobName = Jsoup.parse(html.css("div.cn h1").toString()).text();
        jobInfo.setJobName(jobName);
        String text = Jsoup.parse(html.css("p.ltype").toString()).text();
        String address = text.substring(0, 2);
        //String jobAddr = Jsoup.parse(html.css("div.bmsg p.fp").nodes().get(1).toString()).text();
        jobInfo.setJobAddr(address);
        String jobInfos = Jsoup.parse(html.css("div.job_msg").toString()).text();
        jobInfo.setJobInfo(jobInfos);
        String salary = html.css("div.cn strong", "text").toString();
        if (StringUtils.isNotBlank(salary)){
            Integer[] texts = MathSalary.getSalary(salary);
            jobInfo.setSalaryMin(texts[0]);
            jobInfo.setSalaryMax(texts[1]);
        }
        String url = page.getUrl().toString();
        jobInfo.setUrl(url);
        String times = Jsoup.parse(html.css("p.ltype").toString()).text();
        String[] split = times.split("-");
        String time = split[0].substring(split[0].length() - 2) +"-"+ split[1].substring(0, 2);
        jobInfo.setTime(time);

        //保存(内存)
        page.putField("jobInfo",jobInfo);


    }

    private Site site = Site.me()
            .setCharset("gbk")  //编码
            .setTimeOut(20*1000)  //超时
            .setRetryTimes(5)  //重试次数
            .setRetrySleepTime(3000);   //重试时间间隔

    @Override
    public Site getSite() {
        return site;
    }

    //等待一秒后执行  一百秒执行一次
    @Scheduled(initialDelay = 1000,fixedDelay = 100000)
    public void process(){
        Spider.create(new JobProcessor())
                .addUrl(url)
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100000)))
                .thread(10)
                .addPipeline(this.dataPipline)
                .run();
    }
}
